package com.whh.dao.user;

import com.mysql.jdbc.StringUtils;
import com.whh.util.BaseUtils;
import com.whh.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    //登录
    @Override
    public User login(Connection connection, String userCode) {
        PreparedStatement pst=null;
        ResultSet rs=null;
        User user=null;
        if (connection!=null){
            String sql="select * from smbms_user where userCode=?";
            Object[] params={userCode};

            try {
                rs= BaseUtils.execute(connection,pst,rs,sql,params);
                if (rs.next()){
                    user=new User();
                    user.setId(rs.getInt("id"));
                    user.setUserCode(rs.getString("userCode"));
                    user.setUserName(rs.getString("userName"));
                    user.setUserPassword(rs.getString("userPassword"));
                    user.setGender(rs.getInt("gender"));
                    user.setBirthday(rs.getDate("birthday"));
                    user.setPhone(rs.getString("phone"));
                    user.setAddress(rs.getString("address"));
                    user.setUserRole(rs.getInt("userRole"));
                    user.setCreatedBy(rs.getInt("createdBy"));
                    user.setCreationDate(rs.getTimestamp("creationDate"));
                    user.setModifyBy(rs.getInt("modifyBy"));
                    user.setModifyDate(rs.getTimestamp("modifyDate"));
                }
                BaseUtils.closeResource(null,pst,rs);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return user;
    }
    //修改密码
    @Override
    public int updatePwd(Connection connection, int id, String password) throws SQLException {
        PreparedStatement pstm=null;
        int execute=0;
        if (connection!=null){
            String sql="update smbms_user set userPassword=? where id=?";
            Object[] params={password,id};
            execute= BaseUtils.execute(connection,pstm,sql,params);
            BaseUtils.closeResource(null,pstm,null);
        }
        return execute;
    }
    //根据用户名或者角色查询用户总数
    @Override
    public int getUserCount(Connection connection, String username, int userRole) throws SQLException {
        PreparedStatement pstm=null;
        ResultSet rs=null;
        int count=0;
        if (connection!=null){
            StringBuffer sql=new StringBuffer();
            sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole =r.id");
            ArrayList<Object> list = new ArrayList<>();
            if (!StringUtils.isNullOrEmpty(username)){
                sql.append(" and u.userName like ?");
                list.add("%"+username+"%");
            }

            if (userRole>0){
                sql.append(" and u.userRole=?");
                list.add(userRole);
            }
            Object[] params = list.toArray();
            System.out.println("UserDaoImpl-->getUserCount" + sql.toString());
            rs = BaseUtils.execute(connection, pstm, rs, sql.toString(), params);
            if (rs.next()){
                count=rs.getInt("count");
            }
            BaseUtils.closeResource(null,pstm,rs);

        }
        return count;
    }
    //查询用户列表
    @Override
    public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws SQLException {
        PreparedStatement pstm=null;
        ResultSet rs=null;
        List<User> userList=new ArrayList<>();
        if (connection!=null){
            StringBuffer sql=new StringBuffer();
            sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole =r.id");
            List<Object> list=new ArrayList<>();
            if (!StringUtils.isNullOrEmpty(userName)){
                sql.append(" and u.userName like ?");
                list.add("%"+userName+"%");
            }
            if (userRole>0){
                sql.append(" and u.userRole=?");
                list.add(userRole);
            }
            /*分页*/
            sql.append(" order by creationDate DESC limit ?,?");
            currentPageNo =(currentPageNo-1)*pageSize;
            list.add(currentPageNo);
            list.add(pageSize);

            Object[] params=list.toArray();
            System.out.println("sql-----> "+sql.toString());
            rs = BaseUtils.execute(connection, pstm, rs, sql.toString(), params);
            while (rs.next()){
                User user=new User();
                user.setId(rs.getInt("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setUserRole(rs.getInt("userRole"));
                user.setUserRoleName(rs.getString("userRoleName"));
                userList.add(user);
            }
            BaseUtils.closeResource(null,pstm,rs);
        }
        return userList;
    }

    //添加用户
    @Override
    public int add(Connection connection, User user) throws SQLException {
        PreparedStatement pstm=null;
         int updateRows=0;
        if (connection!=null){
            System.out.println("addUser------->start");
            String sql = "insert into smbms_user(userCode, userName, userPassword, " +
                         "gender, birthday, phone, address, userRole,creationDate,createdBy)" +
                         "value(?,?,?,?,?,?,?,?,?,?)";
            Object[] params={
              user.getUserCode(),user.getUserName(),user.getUserPassword(),
              user.getGender(),user.getBirthday(),user.getPhone(),
              user.getAddress(),user.getUserRole(),user.getCreationDate(),user.getCreatedBy()
            };
            updateRows=BaseUtils.execute(connection,pstm,sql,params);
            BaseUtils.closeResource(null,pstm,null);
        }
        return updateRows;
    }
    //删除用户
    @Override
    public int deleteUserById(Connection connection, Integer delId) throws SQLException {
        int deleteRows=0;
        PreparedStatement pstm=null;
        if (connection!=null){

            String sql="delete from smbms_user where id=?";
            Object[] params={delId};
          deleteRows=  BaseUtils.execute(connection,pstm,sql,params);
            BaseUtils.closeResource(null,pstm,null);
        }
        return deleteRows;
    }
    //修改
    @Override
    public int modify(Connection connection, User user) throws SQLException {
       int updateRows=0;
       PreparedStatement pstm=null;
        if (connection!=null){
            System.out.println("modify----->start");
            String sql="update smbms_user set userName=?," +
                    "gender=?,birthday=?,phone=?,address=?," +
                    "userRole=?,modifyBy=?,modifyDate=? where id =?";;
          Object[] params={user.getUserName(), user.getGender(), user.getBirthday(),
                  user.getPhone(), user.getAddress(), user.getUserRole(), user.getModifyBy(),
                  user.getModifyDate(), user.getId()};

            updateRows=BaseUtils.execute(connection,pstm,sql,params);
            BaseUtils.closeResource(null,pstm,null);
        }
        return updateRows;
    }
    //通过userCode获取User   查询用户是否存在
    @Override
    public User getLoginUser(Connection connection, String userCode) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        try {
            if (null != connection) {
                String sql = "select * from smbms_user where userCode=?";
                Object[] params = {userCode};
                rs = BaseUtils.execute(connection, pstm, rs, sql, params);
                if (rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUserCode(rs.getString("userCode"));
                    user.setUserName(rs.getString("userName"));
                    user.setUserPassword(rs.getString("userPassword"));
                    user.setGender(rs.getInt("gender"));
                    user.setBirthday(rs.getDate("birthday"));
                    user.setPhone(rs.getString("phone"));
                    user.setAddress(rs.getString("address"));
                    user.setUserRole(rs.getInt("userRole"));
                    user.setCreatedBy(rs.getInt("createdBy"));
                    user.setCreationDate(rs.getTimestamp("creationDate"));
                    user.setModifyBy(rs.getInt("modifyBy"));
                    user.setModifyDate(rs.getTimestamp("modifyDate"));
                }
            }
            BaseUtils.closeResource(null, pstm, rs);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }
    //通过userId获取user
    @Override
    public User getUserById(Connection connection, String id) {
      User user =null;
      PreparedStatement pstm=null;
      ResultSet rs=null;
      if (connection!=null){
          try {
              String sql="select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.id=? and u.userRole = r.id";
              Object[] params={id};
              rs=BaseUtils.execute(connection,pstm,rs,sql,params);
              if (rs.next()){
                  user = new User();
                  user.setId(rs.getInt("id"));
                  user.setUserCode(rs.getString("userCode"));
                  user.setUserName(rs.getString("userName"));
                  user.setUserPassword(rs.getString("userPassword"));
                  user.setGender(rs.getInt("gender"));
                  user.setBirthday(rs.getDate("birthday"));
                  user.setPhone(rs.getString("phone"));
                  user.setAddress(rs.getString("address"));
                  user.setUserRole(rs.getInt("userRole"));
                  user.setCreatedBy(rs.getInt("createdBy"));
                  user.setCreationDate(rs.getTimestamp("creationDate"));
                  user.setModifyBy(rs.getInt("modifyBy"));
                  user.setModifyDate(rs.getTimestamp("modifyDate"));
                  user.setUserRoleName(rs.getString("userRoleName"));
              }

          } catch (SQLException throwables) {
              throwables.printStackTrace();
          }finally {
              BaseUtils.closeResource(null,pstm,rs);
          }

      }
        System.out.println(user);
        return user;
    }
}





















































